package com.example.myapplication;
import static com.example.myapplication.BookDatabaseHelper.COLUMN_ID_BOOK;
//import static com.example.myapplication.BorrowDatabaseHelper.COLUMN_BOOK_ID;
import static com.example.myapplication.BorrowDatabaseHelper.COLUMN_BORROW_DATE;
//import static com.example.myapplication.BorrowDatabaseHelper.COLUMN_READER_ID;
import static com.example.myapplication.BorrowDatabaseHelper.TABLE_BORROW_RECORDS;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class ReaderDatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "ReaderDatabase";
    private static final int DATABASE_VERSION = 1;
    public static final String TABLE_READERS = "readers";


    // 列名
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_AGE = "age";
    public static final String COLUMN_GENDER = "gender";
    public static final String COLUMN_CONTACT = "contact";
    public static final String COLUMN_EMAIL = "email";
    public static final String COLUMN_COLLEGE = "college";
    public static final String COLUMN_CLASS = "class";
    public static final String COLUMN_REMARKS = "remarks";

    public ReaderDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTableQuery = "CREATE TABLE " + TABLE_READERS + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_NAME + " TEXT NOT NULL, " +
                COLUMN_AGE + " INTEGER, " +
                COLUMN_GENDER + " TEXT, " +
                COLUMN_CONTACT + " TEXT, " +
                COLUMN_EMAIL + " TEXT, " +
                COLUMN_COLLEGE + " TEXT, " +
                COLUMN_CLASS + " TEXT, " +
                COLUMN_REMARKS + " TEXT)";
        db.execSQL(createTableQuery);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_READERS);
        onCreate(db);
    }

    public long addReader(String name, int age, String gender, String contact,
                          String email, String college, String readerClass, String remarks) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, name);
        values.put(COLUMN_AGE, age);
        values.put(COLUMN_GENDER, gender);
        values.put(COLUMN_CONTACT, contact);
        values.put(COLUMN_EMAIL, email);
        values.put(COLUMN_COLLEGE, college);
        values.put(COLUMN_CLASS, readerClass);
        values.put(COLUMN_REMARKS, remarks);
        long result = db.insert(TABLE_READERS, null, values);
        db.close();
        return result;
    }
    public List<Reader> getAllReaders() {
        List<Reader> readerList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_READERS,
                new String[]{COLUMN_ID, COLUMN_NAME, COLUMN_GENDER, COLUMN_COLLEGE,
                        COLUMN_CLASS, COLUMN_CONTACT}, // 选择需要的列
                null, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                Reader reader = new Reader();
                reader.setId(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));
                reader.setName(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));
                reader.setGender(cursor.getString(cursor.getColumnIndex(COLUMN_GENDER)));
                reader.setCollege(cursor.getString(cursor.getColumnIndex(COLUMN_COLLEGE)));
                reader.setReaderClass(cursor.getString(cursor.getColumnIndex(COLUMN_CLASS)));
                reader.setContact(cursor.getString(cursor.getColumnIndex(COLUMN_CONTACT)));
                // 设置其他需要的字段...
                readerList.add(reader);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return readerList;
    }
    // 检查书籍是否存在
    public boolean isBookAvailable(String bookTitle) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(BookDatabaseHelper.TABLE_BOOKS,
                new String[]{COLUMN_ID_BOOK},
                BookDatabaseHelper.COLUMN_TITLE + "=?",
                new String[]{bookTitle}, null, null, null);

        boolean exists = cursor.getCount() > 0;
        cursor.close();
        db.close();
        return exists;
    }


    // 获取书籍ID
    public int getBookIdByTitle(String bookTitle) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(BookDatabaseHelper.TABLE_BOOKS,
                new String[]{COLUMN_ID_BOOK},
                BookDatabaseHelper.COLUMN_TITLE + "=?",
                new String[]{bookTitle}, null, null, null);

        int bookId = -1;
        if (cursor.moveToFirst()) {
            bookId = cursor.getInt(cursor.getColumnIndex(COLUMN_ID_BOOK));
        }
        cursor.close();
        db.close();
        return bookId;
    }

    public boolean isReaderAvailable(String name) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(
                TABLE_READERS,
                new String[]{COLUMN_ID},
                COLUMN_NAME + " = ?",
                new String[]{name},
                null, null, null
        );

        boolean exists = cursor.getCount() > 0;
        cursor.close();
        db.close();
        return exists;
    }

    public int getReaderIdByName(String name) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(
                TABLE_READERS,
                new String[]{COLUMN_ID},
                COLUMN_NAME + " = ?",
                new String[]{name},
                null, null, null
        );

        int readerId = -1;
        if (cursor.moveToFirst()) {
            readerId = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));
        }

        cursor.close();
        db.close();
        return readerId;
    }
    // 在ReaderDatabaseHelper类中添加以下方法
    public Reader getReaderById(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_READERS,
                null,
                COLUMN_ID + " = ?",
                new String[]{String.valueOf(id)},
                null, null, null);

        Reader reader = null;
        if (cursor.moveToFirst()) {
            reader = new Reader();
            reader.setId(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));
            reader.setName(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));
            reader.setAge(cursor.getInt(cursor.getColumnIndex(COLUMN_AGE)));
            reader.setGender(cursor.getString(cursor.getColumnIndex(COLUMN_GENDER)));
            reader.setContact(cursor.getString(cursor.getColumnIndex(COLUMN_CONTACT)));
            reader.setEmail(cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL)));
            reader.setCollege(cursor.getString(cursor.getColumnIndex(COLUMN_COLLEGE)));
            reader.setReaderClass(cursor.getString(cursor.getColumnIndex(COLUMN_CLASS)));
            reader.setRemarks(cursor.getString(cursor.getColumnIndex(COLUMN_REMARKS)));
        }

        cursor.close();
        db.close();
        return reader;
    }
}